package cn.trigram.controller;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequestMapping("/filepath")
@RestController
public class FilePathController {

  @GetMapping("/thread_path")
  public String getThreadCurrentPath() throws URISyntaxException, IOException {

    return Thread.currentThread().getContextClassLoader()
        .getResource("").getPath();
  }
  @GetMapping("/nio_path")
  public String getNioPath() throws URISyntaxException, IOException {

    URL url = Thread.currentThread().getContextClassLoader().getResource("");
    Path path = Paths.get(url.toURI());
    return path.resolve("../../..").normalize().toAbsolutePath().toString();
  }

}
